/**************************************************************************
 *         Copyright(c) 2007 by Sunplus mMedia  Technology Co., Ltd.       *
 *                                                                         *
 *  This software is copyrighted by and is the property of Sunplus mMedia  *
 *  Technology Co., Ltd. All rights are reserved by Sunplus mMedia Techno- *
 *  logy Co., Ltd. This software may only be used in accordance with the   *
 *  corresponding license agreement. Any unauthorized use, duplication,    *
 *  distribution, or disclosure of this software is expressly forbidden.   *
 *                                                                         *
 *  This Copyright notice "M U S T" not be removed or modified without     *
 *  prior written consent of Sunplus mMedia Technology  Co., Ltd.          *
 *                                                                         *
 *  Sunplus mMedia Technology Co., Ltd. reserves the right to modify this  *
 *  software without notice.                                               *
 *                                                                         *
 *  Sunplus mMedia Technology Co., Ltd.                                    *
 *  19-1, Innovation First Road, Science-Based Industrial Park,            *
 *  Hsin-Chu, Taiwan, R.O.C.                                               *
 *                                                                         *
 **************************************************************************/
 /*
   Note: this file contains apis of calibration menu mode 
 */
#include "general.h"
#include "os_msg.h"
#include "dos32.h"

#include "sp1k_calibration_api.h"//calibration api's prototype
#include "sp1k_snap_api.h"
#include "sp1k_osd_api.h"
#include "sp1k_hal_api.h"
#include "sp1k_bp_api.h"
#include "sp1k_rsvblk_api.h"

#include "app_menu.h"
#include "app_menu_core.h"
#include "app_menu_api.h"
#include "app_menu_tab.h"
#include "app_ui_power.h"
#include "app_audio_play.h"
#include "app_ui_osd.h"
#include "app_ui_para.h"
#include "button_def.h"
#include "initio.h"
#include "Key_scan.h"
#include "bp_private.h"

 /**************************************************************************
 *                      G L O B A L    C O N S T A N T                    *
 **************************************************************************/
/**************************************************************************
 *                         G L O B A L    D A T A                         *
 **************************************************************************/
static bpPara_t code sBpPara = {
	640, /* preview raw image width */
	480, /* preview raw image height */

	2592, /* capture raw image width */
	1944, /* capture raw image height */

	30, /* threshold */

	4096, /* preview badpixel max number */
	4096, /* capture badpixel max number */
	0,
	0,

	0x55aa, /* EOF */
};

static UINT8 id0, id1, id2, id3;	//storage AE Test Gid Step
static UINT8 AEGid = 50;			//when AE manual mode active, record Gid current item

 /**************************************************************************
 *               F U N C T I O N    D E C L A R A T I O N S               *
 **************************************************************************/
static void appRGBvalueShow (UINT8 * value);
static void appShowWait (void);	
static UINT8 appCalStorageChk(void);
UINT8 appBpPro(void);
void appDramReadDWord(UINT32 addr ,UINT16* val_x,UINT16* val_y);
void appDramWriteDWord(UINT32 addr,UINT16 a, UINT16 b);
static void appCardUpdateFW(void);
 /**************************************************************************
 *                 E X T E R N A L    R E F E R E N C E S                 *
 **************************************************************************/
extern UINT8 G_SDCardPlug;
extern UINT8 appPreStateGet(UINT8 PreLev);
extern xdata UINT8 calibrationObData[ SP1K_CALIBRATION_OB_MAX ];
extern xdata UINT8 calibrationAwbData[ SP1K_CALIBRATION_AWB_MAX ];
extern xdata UINT8 calibrationPvObData[ SP1K_CALIBRATION_PV_OB_MAX ];


void sp1kCalCfgDataRst(void)
{
	uiPara_t* puiPara;

	puiPara = appUiParaGet();
	puiPara->cal_ae = 1;
	puiPara->cal_awb = 1;
	puiPara->cal_gma = 1;
	puiPara->cal_ob = 1;
	puiPara->cal_lc = 1;

	/*OB LSC AWB Gamma turning off*/
	sp1kCalibrationObSet(1);
	sp1kCalibrationLcSet(1);
	sp1kCalibrationAESet(1);
	sp1kCalibrationAWBSet(1);
	sp1kCalibrationGammaSet(1);
}


void appCalMenu (UINT8 msg) USING_0
{
	UINT8 layer;
	UINT8 item;
	UINT8 sel = 0;
	UINT8 subItem=0;
	UINT32 menuPos=0;
	uiPara_t* puiPara;

	puiPara = appUiParaGet();
	menuPos = menuProcCurrPosGet();
	layer = menuProcLayerGet();
	item = menuProcCurrItemGet(layer);
	subItem = menuProcSubItemGet(layer, item);

	switch (msg) {
		case SP1K_MSG_STATE_INIT:
#if 0
			if( /*APP_STATE_POWER_ON*/1 == appPreStateGet(0))//USB Power on
			{
				menuExit();//exit menu and turn to still view mode
				G_SDCardPlug = 0xFF;//force remount card or nand flash
				break;
			}
#endif
			appAutoOffEn(0);//disable auto power off

			sp1kCalibrationLoadCriteria( SP1K_CALIBRATION_CRITERIA_FILE_ID );	//read ae test parameters
			sp1kCalibrationAeTestGidStepGet( &id0, &id1, &id2, &id3 );			//and save
			sp1kCalibrationDefaultSizeSet();									//init size

			sp1kCalCfgDataRst();

			menuProcCurrPosSet(MENU_MARK_CALIBRATION_MENU);
			menuProcChild (0);
			menuProcDisp();
			break;

    	case SP1K_MSG_KEY_PRESS_UP:

			menuProcMoveUp ();
			menuProcDisp();
			
			if ((menuPos >= MENU_MARK_CAL_AE_GID_START) && (menuPos < MENU_MARK_CAL_AE_GID_END))	//AE Gid +/-
			{
				item = menuProcCurrItemGet(layer);
				//printf("subItem = %d\n",(int)item);//item
				AEGid = item;

				sp1kCalibrationAEConfig (item, 50);
			}

			break;

    	case SP1K_MSG_KEY_PRESS_DOWN:

			menuProcMoveDown ();
			menuProcDisp();

			if ((menuPos >= MENU_MARK_CAL_AE_GID_START) && (menuPos < MENU_MARK_CAL_AE_GID_END))	//AE Gid +/-
			{
				item = menuProcCurrItemGet(layer);
				//printf("subItem = %d\n",(int)item);//item
				AEGid = item;

				sp1kCalibrationAEConfig (item, 50);
			}

			break;

		case SP1K_MSG_KEY_PRESS_LEFT:

			if ((menuPos >= MENU_MARK_CAL_AE_TEST_1) && (menuPos <= MENU_MARK_CAL_AE_TEST_SAVE))
			{
				sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_OFF );
			}
			

			if (layer >= MENU_LAYER_2)
			{
				menuProcParent ();
				menuProcDisp();
			}
			break;

		case SP1K_MSG_KEY_PRESS_RIGHT:

			if( menuPos == MENU_MARK_CAL_AE_GID)	//action for entering AE Gid
			{
				sel = AEGid;
				menuProcChild (sel);
				menuProcDisp();

				sp1kCalibrationAEConfig (AEGid, 50);
				break;
			}


			if( menuPos == MENU_MARK_CAL_AE_TEST)	//action for entering AE Gid
			{
				sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_AE_TEST );
				//printf( "%s: %ld, idx [%bx][%bx][%bx][%bx]\n", __FILE__, (UINT32)__LINE__, id0, id1, id2, id3 );
			}


			if (subItem > 0)
			{
				sel = menuProcParaGet(layer, item);
				menuProcChild (sel);
				menuProcDisp();
			}

			break;
		
    	case SP1K_MSG_KEY_PRESS_OK:
		case SP1K_MSG_KEY_PRESS_F1:

			if( menuPos == MENU_MARK_USB_POWER)		//USB Power mode
			{
				menuExit();//exit menu and turn to still view mode
				break;
			}



			if( menuPos == MENU_MARK_CAL_OB_ON)		//OB opt
			{
				puiPara->cal_ob = 1;
				sp1kCalibrationObSet(1);
				menuProcParent ();
				menuProcDisp();
				break;
			}
			else if ( menuPos == MENU_MARK_CAL_OB_OFF)
			{
				puiPara->cal_ob = 0;
				sp1kCalibrationObSet(0);
				menuProcParent ();
				menuProcDisp();
				break;
			}



			if (menuPos == MENU_MARK_CAL_LENS_SHADING_ON)	//Lens shading opt
			{
				puiPara->cal_lc = 1;
				sp1kCalibrationLcSet(1); 
				menuProcParent ();
				menuProcDisp();
				break;
			}
			else if (menuPos == MENU_MARK_CAL_LENS_SHADING_OFF)
			{
				puiPara->cal_lc = 0;
				sp1kCalibrationLcSet(0); 
				menuProcParent ();
				menuProcDisp();
				break;
			}




			if (menuPos == MENU_MARK_CAL_GAMMA_ON)	//Gamma opt
			{
				puiPara->cal_gma = 1;
				sp1kCalibrationGammaSet(1);
				menuProcParent ();
				menuProcDisp();
				break;
			}
			else if (menuPos == MENU_MARK_CAL_GAMMA_OFF)
			{
				puiPara->cal_gma = 0;
				sp1kCalibrationGammaSet(0);
				menuProcParent ();
				menuProcDisp();
				break;
			}



			if (menuPos == MENU_MARK_CAL_AE_ON)	//AE opt
			{
				puiPara->cal_ae = 1;
				sp1kCalibrationAESet(1);
				menuProcParent ();
				menuProcDisp();
				break;
			}
			else if (menuPos == MENU_MARK_CAL_AE_OFF)
			{
				puiPara->cal_ae = 0;
				sp1kCalibrationAESet(0);
				menuProcParent ();
				menuProcDisp();
				break;
			}




			if ((menuPos >= MENU_MARK_CAL_AE_GID_START) && (menuPos < MENU_MARK_CAL_AE_GID_END))	//AE Gid +/-
			{
				//AEGid = item;
				menuProcParent ();
				menuProcDisp();
				break;
			}



			if (menuPos == MENU_MARK_CAL_AWB_ON)	//AWB opt
			{
				puiPara->cal_awb = 1;
				sp1kCalibrationAWBSet(1); 
				menuProcParent ();
				menuProcDisp();
				break;
			}
			else if (menuPos == MENU_MARK_CAL_AWB_OFF)
			{
				puiPara->cal_awb = 0;
				sp1kCalibrationAWBSet(0); 
				menuProcParent ();
				menuProcDisp();
				break;
			}



			if (menuPos == MENU_MARK_CAL_PV_RAW)	//PV raw
			{
				sp1kCalibrationPvRaw();
				break;
			}



			if (menuPos == MENU_MARK_CAL_AE_TEST_1)	//AE Test
			{
				appShowWait();
				sp1kCalibrationAETest( id0, id1 - 1, 60 );
				menuProcDisp();
				break;
			}
			else if (menuPos == MENU_MARK_CAL_AE_TEST_2)
			{
				appShowWait();
				sp1kCalibrationAETest( id1, id2 - 1, 60 );
				menuProcDisp();
				break;
			}
			else if (menuPos == MENU_MARK_CAL_AE_TEST_3)
			{
				appShowWait();
				sp1kCalibrationAETest( id2, id3 - 1, 60 );
				menuProcDisp();
				break;
			}
			else if (menuPos == MENU_MARK_CAL_AE_TEST_SAVE)
			{
				sp1kCalibrationAETestSave( id0, id3 - 1 );
				sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_OFF );
				menuProcParent ();
				menuProcDisp();
				break;
			}


			if (menuPos == MENU_MARK_CAL_OB)	//calibration OB
			{
				sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_OB );
				//sp1kCalibrationObSet(0);	//set to manual OB mode

				// snap
				//for (AEGid = 0; AEGid < 101; AEGid++)
				{
					appVoicePlay(SP1K_MSG_UNKNOWN, VOICE_TYPE_SHUTTER, VOICE_PLAYLEVLE_HIGH);
					sp1kSnapTask();

					appRGBvalueShow(calibrationObData);
					
					sp1kCalibrationSaveIq( SP1K_CALIBRATION_OB_FILE_ID );
					sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_OFF );
				}

				//restore ob configure
				//sp1kCalibrationObSet(puiPara->cal_ob);
				break;
			}

			if (menuPos == MENU_MARK_CAL_AUTO_OB)	//calibration OB
			{
				UINT8 gid_bak;
				
				sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_OB );
				sp1kSnapParamSet( SP1K_SNAP_PARAM_SKIP_STORE, 1 ); //don not store
				sp1kCalibrationObSet(0);	//set to manual OB mode

				//backup gid value
				gid_bak = AEGid;
				
				// snap
				for (AEGid = 0; AEGid < 101; AEGid++)
				{
					appVoicePlay(SP1K_MSG_UNKNOWN, VOICE_TYPE_SHUTTER, VOICE_PLAYLEVLE_HIGH);
					sp1kSnapTask();

					//appRGBvalueShow(calibrationObData);
					//printf("AEGid / R / Gr / B / Gb = %d / %d / %d / %d / %d\n",(int)AEGid,(int)calibrationObData[0],(int)calibrationObData[1],(int)calibrationObData[2],(int)calibrationObData[3]);
					
					sp1kCalibrationSaveIq( SP1K_CALIBRATION_OB_FILE_ID );
					sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_OFF );
				}

				//restore configure
				sp1kCalibrationObSet(puiPara->cal_ob);
				sp1kSnapParamSet( SP1K_SNAP_PARAM_SKIP_STORE, 0 ); //re-open store
				AEGid = gid_bak;
				
				break;
			}


			if (menuPos == MENU_MARK_PV_OB)	//calibration OB
			{
				sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_PV_OB ); 

				// snap
				//appVoicePlay(SP1K_MSG_UNKNOWN, VOICE_TYPE_SHUTTER, VOICE_PLAYLEVLE_HIGH);
				//sp1kSnapTask();

				sp1kCalibrationPvRaw(); 
				
				sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_OFF ); 
				appRGBvalueShow(calibrationPvObData);
				break;
			}




			if (menuPos == MENU_MARK_CAL_AWB)	//awb calibration
			{
				sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_AWB );

				// snap
				appVoicePlay(SP1K_MSG_UNKNOWN, VOICE_TYPE_SHUTTER, VOICE_PLAYLEVLE_HIGH);
				sp1kSnapTask();

				appRGBvalueShow(calibrationAwbData);
				
				//sp1kCalibrationSaveIq( SP1K_CALIBRATION_AWB_FILE_ID );
				sp1kCalibrationModeSet( SP1K_CALIBRATION_MODE_OFF );

				break;
			}



			if (menuPos == MENU_MARK_CAL_SNAP_RAW)	//snap raw
			{
				sp1kSnapParamSet( SP1K_SNAP_PARAM_SAVE_RAW_OPT, 1 ); 
				appVoicePlay(SP1K_MSG_UNKNOWN, VOICE_TYPE_SHUTTER, VOICE_PLAYLEVLE_HIGH);
				sp1kSnapTask();
				break;
			}

			
			if (menuPos == MENU_MARK_CAL_SNAP_PURE_RAW)	//snap raw
			{
				/*OB LSC AWB Gamma turning off*/
				sp1kCalibrationObSet(0);
				sp1kCalibrationLcSet(0);
				sp1kCalibrationAWBSet(0);
				sp1kCalibrationGammaSet(0);
				
				sp1kSnapParamSet( SP1K_SNAP_PARAM_SAVE_RAW_OPT, 1 ); 
				appVoicePlay(SP1K_MSG_UNKNOWN, VOICE_TYPE_SHUTTER, VOICE_PLAYLEVLE_HIGH);
				sp1kSnapTask();

				sp1kCalibrationObSet(puiPara->cal_ob);
				sp1kCalibrationLcSet(puiPara->cal_lc);
				sp1kCalibrationAWBSet(puiPara->cal_awb);
				sp1kCalibrationGammaSet(puiPara->cal_gma);
				break;
			}


			if (menuPos == MENU_MARK_CAL_PV_YUV)	//pv YUV
			{
				sp1kCalibrationPvYuv();
				break;
			}
			
			if (menuPos == MENU_MARK_CAL_BP_PRV)	/* preview badpixel compesation */
			{
				if ( appCalStorageChk() == SUCCESS ) {
					appShowWait();
					sp1kBpCfg(&sBpPara);
					sp1kCalBpProc(BP_MODE_PRV);
				}
				menuProcDisp();
				break;
			}
			if (menuPos == MENU_MARK_CAL_BP_CAP)	/* capture badpixel compesation */
			{
				if ( appCalStorageChk() == SUCCESS ) {
					appShowWait();
					sp1kBpCfg(&sBpPara);
					sp1kCalBpProc(BP_MODE_CAP);
				}
				menuProcDisp();
				break;
			}
			if (menuPos == MENU_MARK_CAL_BP_ALL)	/* Prv&Cap badpixel compesation */
			{
				if ( appCalStorageChk() == SUCCESS ) {
					sp1kOsdClear(0, (1<<4)+8);
					sp1kOsdSelWinSet(-1, -1, -1, -1, 0, -1);	
					sp1kOsdStrDraw(0,0,"Pls Capture a white  image",2);
					while (sp1kBtnChk(SP1K_BTN_S2) != KEY_STATUS_PRESSED);
					appShowWait();
					sp1kBpParaSet(0,0x179,0x08);
					sp1kBpCfg(&sBpPara);
					sp1kCalBpProc(BP_MODE_PRV);
					appShowWait();
					sp1kBpParaSet(0,0x179,0x08);
					sp1kCalBpProc(BP_MODE_CAP);
					#if 1// black badpixel
					if ( sp1kBpResCfg(RES_BP_CAP, 0x45) == SUCCESS ) {
						if ( sp1kBpResLoad(RES_BP_CAP, K_SDRAM_BadPixelCoordBufAddr + K_SDRAM_BadPixel_PrvBufSize) != 0 ) {
							sp1kSnapParamSet(SP1K_SNAP_PARAM_BAD_PIXEL_OPT, 1);
						}
					}
					sp1kOsdStrDraw(0,0,"Pls Capture a black image",2);
					while (sp1kBtnChk(SP1K_BTN_S2) != KEY_STATUS_PRESSED);
					sp1kBpParaSet(1,0xc8,0x1a);
					sp1kBpCfg(&sBpPara);
					appShowWait();
					sp1kCalBpProc(BP_MODE_CAP);

					if(appBpPro() ==0)printf("Bp FAIL\n");
					#endif
					
				}
				menuProcDisp();
				break;
			}
				
			if(menuPos == MENU_MARK_FW_UPGRADE){
				menuProcDisp();
				appCardUpdateFW();
				osMsgPost(SP1K_MSG_POWER_OFF);
				break;
			}	
			break;


		case SP1K_MSG_CARD_PLUG_IN:

			appStorageMount(K_MEDIA_SDF);
			break;


		case SP1K_MSG_CARD_PLUG_OUT:

			appStorageMount(K_MEDIA_NANDF);
			break;

		case SP1K_MSG_USB_PLUG_IN:
			sp1kHalUSBSwPlugIn();
			break;
		case SP1K_MSG_POWER_OFF:
			appPowerOffProc();
			break;
		case SP1K_MSG_USB_PLUG_OUT:
			break;
	}
}

//UINT16 val_x,val_y;

UINT8 appBpPro(void)
{
	UINT8 bad_idx0= 0x45,bad_idx1= 0x06;
	UINT32 startAddr = K_SDRAM_CommonEndAddr+(UINT32)1024*64;
	UINT32 idxSize=32768;
	UINT32 badAddr0= startAddr,badAddr1= startAddr + idxSize/2,dstAddr=  startAddr + idxSize;
	UINT8 sts=0;
	static UINT16 x0,y0,x1,y1;
	UINT32 cnt=0;
	const UINT8 bInitVal = 0xfe;
	
	/* buffer init */
	HAL_FillLongDRAM(badAddr0, idxSize, bInitVal, bInitVal);
	HAL_FillLongDRAM(badAddr1, idxSize, bInitVal, bInitVal);
	HAL_FillLongDRAM(dstAddr, idxSize, bInitVal, bInitVal);
	
	sts = sp1kNandRsvRead(bad_idx0,badAddr0<<1);
	//RD_Dram2Card_MakeFile("SRC0000BBIN", badAddr0, idxSize);
	if(sts){
		sts=sp1kNandRsvRead(bad_idx1,badAddr1<<1);
		//RD_Dram2Card_MakeFile("SRC0000WBIN", badAddr1, idxSize);
		if(sts ==0){
			return sts;
		}
	}
	else{
		return sts;
	}

	start:
	while(badAddr0< (startAddr+ idxSize/2)){
		appDramReadDWord(badAddr0,&x0,&y0);
		if(x0 == 0xffff && y0==0xffff){
			while(1){
				appDramReadDWord(badAddr1,&x1,&y1);
				appDramWriteDWord(dstAddr, x1, y1);
				badAddr1+=2;
				dstAddr+=2;
				if(badAddr1 >= startAddr + idxSize)goto save;
			}
			
		}
		while(badAddr1 < startAddr + idxSize){
			appDramReadDWord(badAddr1,&x1,&y1);
			if(x1 == 0xffff && y1==0xffff){
				while(1){
					appDramReadDWord(badAddr0,&x0,&y0);				
					appDramWriteDWord(dstAddr, x0, y0);
					badAddr0+=2;
					dstAddr+=2;
					if(badAddr0 == startAddr + idxSize/2)goto save;
				}
			}
			if(y0<y1){
				appDramWriteDWord(dstAddr, x0, y0);
				badAddr0+=2;
				dstAddr+=2;
				cnt+=4;
				if(cnt == idxSize-4){
					appDramWriteDWord(dstAddr, 0xffff, 0xffff);
					goto save;
				}
				goto start;
			}else if(y0==y1){
				if(x0<x1){
					appDramWriteDWord(dstAddr, x0, y0);
					badAddr0+=2;
					dstAddr+=2;
					cnt+=4;
					if(cnt == idxSize-4){
						appDramWriteDWord(dstAddr, 0xffff, 0xffff);
						goto save;
					}
					goto start;
				}else if(x0==x1){
					appDramWriteDWord(dstAddr, x0, y0);
					badAddr0+=2;
					badAddr1+=2;
					dstAddr+=2;
					cnt+=4;
					if(cnt == idxSize-4){
						appDramWriteDWord(dstAddr, 0xffff, 0xffff);
						goto save;
					}
					goto start;
				}else /*x0>x1*/{
					appDramWriteDWord(dstAddr, x1, y1);
					badAddr1+=2;
					dstAddr+=2;
					cnt+=4;
					if(cnt == idxSize-4){
						appDramWriteDWord(dstAddr, 0xffff, 0xffff);
						goto save;
					}
				}
				
			}else if(y0 > y1){ 
				appDramWriteDWord(dstAddr, x1, y1);
				badAddr1+=2;
				dstAddr+=2;
				cnt+=4;
				if(cnt == idxSize-4){
					appDramWriteDWord(dstAddr, 0xffff, 0xffff);
					goto save;
				}
			}	
			
		}
	}
save:
	//RD_Dram2Card_MakeFile("SSS00006BIN", (startAddr+ idxSize), idxSize);
	sp1kNandRsvWrite(0x06,(startAddr+ idxSize)<<1);
return sts;	
	
}

void appDramReadDWord(UINT32 addr ,UINT16* val_x,UINT16* val_y)
{
	UINT8 tmp0,tmp1;
	HAL_DRAMSetStartAddr(addr, 1);
	HAL_DRAMReadWord(&tmp0, &tmp1);
	*val_x=(UINT16)tmp1 | ((UINT16)tmp0<<8);
	HAL_DRAMReadWord(&tmp0, &tmp1);
	*val_y =(UINT16) tmp1 | ((UINT16)tmp0<<8);
	//printf("X:%x,Y:%x\n",*val_x,*val_y );
}

void appDramWriteDWord(UINT32 addr,UINT16 a, UINT16 b)
{
	BP_DRAM_SetStartAddress(addr, 0);
	BP_DRAM_WriteWord( (a>>8)&0xff,a&0xff );
	BP_DRAM_WriteWord( (b>>8)&0xff,b&0xff );
}

static void appRGBvalueShow (UINT8 * value)
{

	UINT8 str[10];

	sp1kOsdClear(0, (1<<4)+8);
	sp1kOsdSelWinSet(-1, -1, -1, -1, 0, -1);
	sp1kOsdStrDraw(0,0,"Up/Down : Exit",(1<<4)+8);
	sprintf(str, "R : %d", (int)value[0]);
	sp1kOsdStrDraw(0,2,str,(1<<4)+8);
	sprintf(str, "Gr : %d", (int)value[1]);
	sp1kOsdStrDraw(0,3,str,(1<<4)+8);
	sprintf(str, "B : %d", (int)value[2]);
	sp1kOsdStrDraw(0,4,str,(1<<4)+8);
	sprintf(str, "Gb : %d", (int)value[3]);
	sp1kOsdStrDraw(0,5,str,(1<<4)+8);

}


static void appShowWait(void)
{
	sp1kOsdClear(0, (1<<4)+8);
	sp1kOsdSelWinSet(-1, -1, -1, -1, 0, -1);
	sp1kOsdStrDraw(0,0,"waiting...",(1<<4)+8);
}

static UINT8 appCalStorageChk(void)
{
	devAttr_t* pDevAttr;
	UINT8 devSts = DEV_STS_UNKNOWN;

	pDevAttr = sp1kDevHdlGet();
	
	/* check the active storage device */
	if ( appSDPlugStatusGet() == 1 ) {
		pDevAttr += DEV_ID_CARD;
	} else {
		pDevAttr += DEV_ID_DISK;
	}

	/* check device status */
	switch ( pDevAttr->devSts ) {
		case DEV_STS_RDY:
			devSts = DEV_STS_RDY;
			break;
		case DEV_STS_ERR:
			devSts = DEV_STS_ERR;
			break;
		case DEV_STS_UNKNOWN:
		case DEV_STS_BUSY:
		case DEV_STS_CONNECTED:
		case DEV_STS_DISCONNECTED:
		default:
			devSts = DEV_STS_UNKNOWN;
			break;
	}

	/* if the storage is not in ready */
	if ( devSts != DEV_STS_RDY ) {
		/* if there's card insert */
		if ( appSDPlugStatusGet() == 1 ) {
//			printf("Card err\n");
			uiOsdDialogDisp(ID_STR_CARD_ERROR,1000);
		} else {
			/* the internal memory is active */
			if ( devSts == DEV_STS_ERR ) {
//				printf("Disk err\n");
				uiOsdDialogDisp(ID_STR_CARD_ERROR,1000);
			} else {
				/* the internal memory status is unknown */
//				printf("Pls insert card\n");
				uiOsdDialogDisp(ID_STR_NO_CARD_,1000);
			}
		}
	}
	
	if ( pDevAttr->devSts != DEV_STS_RDY ) {
		return FAIL;			
	} else {
		return SUCCESS;
	}
}

#if 1
//Suny add for FW update from SD card, B.
static void appCardUpdateFW(void)
{
	UINT8 OsdMaxX, OsdMaxY;

	sp1kOsdLayoutAttrGet(&OsdMaxX, &OsdMaxY);
	if (1) {
		//sp1kOsdClear(ID_ICON_NONE, 0);
		sp1kOsdStrDraw((OsdMaxX >> 1) - 3, OsdMaxY >> 1, "ISP...", 0);
		if(sp1kCardFirmwareUpdate("D:\\1528.BIN" , 0)==FAIL){
			sp1kOsdStrDraw((OsdMaxX >> 1) - 3, OsdMaxY >> 1, "     ", 0);
		}else{
			sp1kOsdStrDraw((OsdMaxX >> 1) - 3, OsdMaxY >> 1, "ISP DONE", 0);
			sp1kHalWait(500);
			appPowerShutDown();
		}
	}
}
//Suny add for FW update from SD card, E.
#endif
